package org.jeecgframework.web.material.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jeecgframework.core.aop.EhcacheAspect;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.ComboTree;
import org.jeecgframework.core.common.model.json.TreeGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.LogUtil;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.vo.easyui.TreeGridModel;
import org.jeecgframework.web.material.entity.PTypeEntity;
import org.jeecgframework.web.material.service.PTypeServiceI;
import org.jeecgframework.web.system.pojo.base.TSUser;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



/**   
 * @Description: 产品分类
 * @date 2015-08-18 14:11:27
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/pTypeController")
public class PTypeController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(PTypeController.class);

	@Autowired
	private PTypeServiceI pTypeService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "pType")
	public ModelAndView pType(HttpServletRequest request) {
		return new ModelAndView("material/pType/pTypeList");
	}
	
	/**
	 *地域列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(PTypeEntity ptype, HttpServletRequest req) {
		//
		String typeid = req.getParameter("id");
		if (typeid != null) {
			ptype = systemService.getEntity(PTypeEntity.class, typeid);
			req.setAttribute("ptype", ptype);
		}
		//父节点不为空，选择的是树形的子节点
		if(ptype.getParentId()!=null && ptype.getParentId().getId()!=null){
			String pid = ptype.getParentId().getId();
			ptype.setParentId((PTypeEntity)systemService.getEntity(PTypeEntity.class, pid));
			req.setAttribute("ptype", ptype);
		}
		return new ModelAndView("material/pType/pType");
	}
	
	/**
	 * 产品分类列表
	 */
	@RequestMapping(params = "ptypeGrid")
	@ResponseBody
	public List<TreeGrid> ptypeGrid(HttpServletRequest request, TreeGrid treegrid) {
		CriteriaQuery cq = new CriteriaQuery(PTypeEntity.class);
			if (treegrid.getId() != null) {
				cq.eq("parentId.id", treegrid.getId());
			}
			//默认显示最顶级
			if (treegrid.getId() == null) {
				cq.isNull("parentId.id");//这个是最顶层
			}
		
		cq.add();
		List<PTypeEntity> ptypeList = systemService.getListByCriteriaQuery(cq, false);
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		TreeGridModel treeGridModel = new TreeGridModel();
		treeGridModel.setIcon("");
		treeGridModel.setIdField("id");
		treeGridModel.setTextField("name");
		treeGridModel.setParentId("parentId_id");
		treeGridModel.setParentText("parentId_name");
		treeGridModel.setSrc("description");
		treeGridModel.setChildList("childPtypes");
		treeGrids = systemService.treegrid(ptypeList, treeGridModel);
		return treeGrids;
	}
	
	/**
	 * 树形下拉菜单
	 */
	@RequestMapping(params = "getComboTree")
	@ResponseBody
	public List<ComboTree> setPTerritory(HttpServletRequest request, ComboTree comboTree) {
		
		return this.pTypeService.getComboTree(comboTree);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping(params = "savePtype")
	@ResponseBody
	public AjaxJson saveTerritory(PTypeEntity ptype, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		
		if (ptype.getParentId()!=null&&StringUtil.isNotEmpty(ptype.getParentId().getId())) {
			PTypeEntity parent = systemService.getEntity(PTypeEntity.class, ptype.getParentId().getId());
			ptype.setLevel(parent.getLevel().intValue()+1);
		}else{
			ptype.setParentId(null);//强制为null 避免parentId为""的情况
			ptype.setLevel(1);
		}
		if (StringUtil.isNotEmpty(ptype.getId())) {
			message = "产品类型: " + ptype.getName() + "被更新成功";
			systemService.saveOrUpdate(ptype);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);  
		} else {
			HttpSession session = request.getSession();
			TSUser user = (TSUser)session.getAttribute("user");
			ptype.setCreateMan(user);
			ptype.setCreateDate(new Date());
			message = "产品类型: " + ptype.getName() + "被添加成功";
			systemService.save(ptype);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);

        }

        j.setMsg(message);
		
		return j;
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(PTypeEntity ptype, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		ptype = systemService.getEntity(PTypeEntity.class, ptype.getId());
		message = "产品类型: " + ptype.getName() + "被删除成功";
		systemService.delete(ptype);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);

        j.setMsg(message);
		return j;
	}
	
	/**
	 * 测试缓存
	 */
	@RequestMapping(params = "testCache")
	@ResponseBody
	public void testCache(HttpServletRequest request, TreeGrid treegrid) {
//		ComboTree comboTree = new ComboTree();
//		pTypeService.getComboTree(comboTree);
//		PTypeEntity ptypeEntity = pTypeService.getEntity(PTypeEntity.class, "1");
//		ptypeEntity.setDescription("1111111");
//		pTypeService.saveOrUpdate(ptypeEntity);
//		pTypeService.getComboTree(comboTree);
//		LogUtil.info("11111111");
//		LogUtil.error("11111111");
//		LogUtil.debug("11111111");
		EhcacheAspect.removeCache("PTypeEntityCache");
		
	}
}
